package top.houry.test;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import top.houry.grpc.proto.OpenAPIGrpc;
import top.houry.grpc.proto.TestGrpc;

import java.io.IOException;

public class ClientStreamingServiceGrpc {
    public static void main(String[] args) throws Exception {
        Server server = ServerBuilder.forPort(9999).addService(new GrpcService()).build();
        server.start();
        server.awaitTermination();;
    }
}


class GrpcService extends OpenAPIGrpc.OpenAPIImplBase {
    @Override
    public StreamObserver<TestGrpc.Request> clientStreaming(StreamObserver<TestGrpc.Response> responseObserver) {
        return new StreamObserver<TestGrpc.Request>() {
            @Override
            public void onNext(TestGrpc.Request value) {
                System.out.println("收到客户端请求:" + value.getName());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println("错误信息:"+t.getMessage());
            }

            @Override
            public void onCompleted() {
                TestGrpc.Response response = TestGrpc.Response.newBuilder()
                        .setMessage("消息处理完成")
                        .build();
                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
        };
    }
}
